//
//  HDCFFundDetailFooter.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/1.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFFundDetailFooter.h"
#import "HDCFDetailFooterOne.h"

@interface HDCFFundDetailFooter ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *table;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSDictionary *fundTypeDict;
@end

@implementation HDCFFundDetailFooter

- (NSDictionary *)fundTypeDict{
    if (!_fundTypeDict) {
        self.fundTypeDict = @{@"MM":@"货币型",
                              @"BOND":@"债券型",
                              @"MIXED":@"混合型",
                              @"CP":@"保本型",
                              @"EQ":@"股票型",
                              @"AI":@"另类型",
                              @"INDEX":@"指数型",
                              @"ST":@"分级型",
                              @"UNKNOWN":@"其他"
                              };
    }
    return _fundTypeDict;
}

- (void)setDataDict:(NSDictionary *)dataDict{
    _dataDict = dataDict;
    [self.table reloadData];
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        self.titleArray = [NSArray arrayWithObjects:@"基金简称", @"基金全称", @"基金公司", @"成立日期", @"基金代码", @"基金类型", @"基金状态", @"交易状态", @"管理费", @"托管费", @"首募规模", @"最新规模", @"托管银行", nil];
    }
    return _titleArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect middleTableFrame = CGRectMake(0, 1, screenWidth, frame.size.height);
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, screenWidth, 1);
        view.backgroundColor = ZKcolor(245, 245, 245);
        [self addSubview:view];
        UITableView *middleTable = [[UITableView alloc] initWithFrame:middleTableFrame style:UITableViewStylePlain];
        middleTable.tag = 10;
        self.table = middleTable;
        middleTable.scrollEnabled = NO;
        middleTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        middleTable.delegate = self;
        middleTable.dataSource = self;
        [self addSubview:middleTable];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 13;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cellid";
    
    HDCFDetailFooterOne *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [HDCFDetailFooterOne HDCFDetailFooterOne];
    }
    NSString *title = self.titleArray[indexPath.row];
    cell.typeL.text = title;
    
    if (indexPath.row == 7) {
        cell.btnOneWidth.constant = 80;
        cell.btnOne.titleLabel.lineBreakMode = 1;
    }else{
        cell.btnOneWidth.constant = screenWidth - 115;
        cell.btnOne.titleLabel.lineBreakMode = 0;
    }
    
    if (indexPath.row == 0) {
        [cell.btnOne setTitle:_dataDict[@"fundName"] forState:UIControlStateNormal];
        
    }
    if (indexPath.row == 1) {
         [cell.btnOne setTitle:_dataDict[@"fundFullName"] forState:UIControlStateNormal];
        
    }
    if (indexPath.row == 2) {
        [cell.btnOne setTitle:_dataDict[@"fundHouse"] forState:UIControlStateNormal];
    }
    if (indexPath.row == 3) {
        NSNumber *navDate = _dataDict[@"launchDate"];
        NSDateFormatter *stampFormatter = [[NSDateFormatter alloc] init];
        [stampFormatter setDateFormat:@"YYYY-MM-dd"];
        NSDate *stampDate2 = [NSDate dateWithTimeIntervalSince1970:[navDate doubleValue] / 1000];
        NSString *dateString = [stampFormatter stringFromDate:stampDate2];
        [cell.btnOne setTitle:dateString forState:UIControlStateNormal];
    }
//
    if (indexPath.row == 4) {
        [cell.btnOne setTitle:_dataDict[@"fundCode"] forState:UIControlStateNormal];
    }
    if (indexPath.row == 5) {
        [cell.btnOne setTitle:self.fundTypeDict[_dataDict[@"fundType"]] forState:UIControlStateNormal];
    }
    if (indexPath.row == 6) {
        NSString *fundStatue = _dataDict[@"fundStatus"];
        if ([fundStatue isEqualToString:@"0"]) {
            [cell.btnOne setTitle:@"募集期基金" forState:UIControlStateNormal];
        }
        if ([fundStatue isEqualToString:@"1"]) {
            [cell.btnOne setTitle:@"申购期基金" forState:UIControlStateNormal];
        }
        if ([fundStatue isEqualToString:@"2"]) {
            [cell.btnOne setTitle:@"封闭期基金" forState:UIControlStateNormal];
        }
        if ([fundStatue isEqualToString:@"3"]) {
            [cell.btnOne setTitle:@"已清盘基金" forState:UIControlStateNormal];
        }
    }
    
    if (indexPath.row == 7) {
        NSString *isBuyEnable = _dataDict[@"isBuyEnable"];
        NSString *isRspEnable = _dataDict[@"isRspEnable"];
        NSString *isSellEnable = _dataDict[@"isSellEnable"];
        
        cell.btnTwo.hidden = NO;
        cell.btnThree.hidden = NO;
        
        if ([isBuyEnable isEqualToString:@"1"]) {
            [cell.btnOne setTitle:@"不可申购" forState:UIControlStateNormal];
        }else{
            [cell.btnOne setTitle:@"可申购" forState:UIControlStateNormal];
        }
        
        if ([isRspEnable isEqualToString:@"1"]) {
            [cell.btnTwo setTitle:@"不可赎回" forState:UIControlStateNormal];
           
        }else{
             [cell.btnTwo setTitle:@"可赎回" forState:UIControlStateNormal];
        }
        
        if ([isSellEnable isEqualToString:@"1"]) {
            [cell.btnThree setTitle:@"不可定投" forState:UIControlStateNormal];
        }else{
            [cell.btnThree setTitle:@"可定投" forState:UIControlStateNormal];
        }
    }
    if (indexPath.row == 8) {
        NSNumber *managementFee = _dataDict[@"managementFee"];
        NSString *str = [NSString stringWithFormat:@"%.2f%%", managementFee.doubleValue];
        [cell.btnOne setTitle:str forState:UIControlStateNormal];
    }
    
    if (indexPath.row == 9) {
        NSNumber *custodianFee = _dataDict[@"custodianFee"];
        NSString *str = [NSString stringWithFormat:@"%.2f%%", custodianFee.doubleValue];
        [cell.btnOne setTitle:str forState:UIControlStateNormal];
    }
    
    if (indexPath.row == 10) {
        NSNumber *launchSize = _dataDict[@"launchSize"];
        if (launchSize.doubleValue > 0) {
            NSString *str = [NSString stringWithFormat:@"%.4f亿元", launchSize.doubleValue];
            [cell.btnOne setTitle:str forState:UIControlStateNormal];
        }else{
            NSString *str = [NSString stringWithFormat:@"0亿元"];
            [cell.btnOne setTitle:str forState:UIControlStateNormal];
        }
        
    }
    if (indexPath.row == 11) {
        if (![_dataDict[@"fundSize"] isKindOfClass:[NSNull class]]) {
            
            NSNumber *fundSize = _dataDict[@"fundSize"];
            if (fundSize.doubleValue > 0) {
                NSString *str = [NSString stringWithFormat:@"%.4f亿元", fundSize.doubleValue];
                [cell.btnOne setTitle:str forState:UIControlStateNormal];
            }else{
                NSString *str = [NSString stringWithFormat:@"0亿元"];
                [cell.btnOne setTitle:str forState:UIControlStateNormal];
            }
            
        }else{
            [cell.btnOne setTitle:@"暂无信息" forState:UIControlStateNormal];
        }
        
    }
    if (indexPath.row == 12) {
        
        [cell.btnOne setTitle:_dataDict[@"fundCustodian"] forState:UIControlStateNormal];
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        return 50;
    }else{
        return 44;
    }
    
}

@end
